#!/usr/bin/perl
use strict;
use warnings;
use CGI ();

#use CGI::Carp qw(fatalsToBrowser);
use MIME::Lite ();
use Contact;
use Storable qw(lock_store);
my $sender_email          = "\"Woei Shyang\" <woei\@flinders.ocfaustralia.org>";
my $error_email           = "\"Woei Shyang\" <woei\@flinders.ocfaustralia.org>";
my $debug_email           = "\"Woei Shyang\" <woei\@flinders.ocfaustralia.org>";
my @contact_fields        = ( 'name', 'family_name', 'email', 'mobile', 'country' );
my @unique_contact_fields = ( 'email', 'mobile' );
my $cgi                   = new CGI;
my $rv;
my $contactData = {};


while (my $line = <>) {
    chomp($line);
    
    my $c = Contact->new('email' => $line);
    my $rv;
    $rv = $c->load( 'speculative' => 1 );
    if (!$rv) {
        # existing value doesn't exist
        my $uname = $line;
        $uname =~ s/\@.+//;
        $c->name($uname);
        $c->family_name('');
        $c->email($line);
        $c->mobile('');
        $c->country('');
    }
    sendWelcomeEmail({'name' => $c->email, 'email' => $c->email});
    $c->save();
    print $line . " processed\n";
}

sub sendWelcomeEmail {
  my $params = shift              || {};
  my $name   = $params->{'name'}  || '';
  my $email  = $params->{'email'} || '';
  my $bodytext = "Hey $name,

Flinders OCFers!

Just 5 more months until convention! Feeling excited?

The convention committee will be sending out updates via email in the lead up towards the convention. If you are keen on receiving these updates, please visit http://www.ocfconvention.org and enter your details to sign up for the mailing list. 

If you encounter any errors or glitches while navigating through the website, please don't hesitate in letting me know as well :)

God bless!
Woei Shyang
";
  my $msg = new MIME::Lite( From => $sender_email, To => "\"$name\" <$email>", Subject => "Reaching out, building up, and sending back", Type => "TEXT", Data => $bodytext, );
  $msg->send();
} ## end sub sendConfirmationEmail
